๐ Rat Maze - Find All Paths
Discover all possible paths from start to destination using backtracking with UDLR directions.
๐ Find All Paths Challenge
The Problem:
A rat is placed at (0, 0) in an NรN maze. Find ALL possible paths to reach (N-1, N-1).
Rules:
- Maze: NรN matrix (1 = open, 0 = blocked)
- Start: (0, 0), Goal: (N-1, N-1)
- Moves: U (up), D (down), L (left), R (right)
- No cell can be visited twice in same path
- Return paths in lexicographical order
- If no path exists, return -1
Input/Output Format
- Input: N (size), followed by NรN matrix
- Output: All paths as space-separated strings (e.g., "DDRDRR DRDDRR") or "-1"
- Constraints: 2 โค N โค 5, 0 โค m[i][j] โค 1
Example: 4ร4 Maze
Input Maze
S
0
0
0
1
1
0
1
1
1
0
0
0
1
1
E
Output: 2 Paths Found
Path 1: DDRDRR
Path 2: DRDDRR
๐ Backtracking Algorithm
Algorithm Steps
- Initialize: Start at (0,0), mark as visited
- Base Case: If at (N-1, N-1), add current path to result
- Try All Directions: In order D, L, R, U (lexicographical):
- Check if move is valid (in bounds, open, not visited)
- Mark cell as visited, add direction to path
- Recursively explore from new position
- Backtrack: unmark cell, remove direction
- Sort: Paths automatically sorted due to DโLโRโU order
- Return: All paths or "-1" if none found
Key Difference from Single Path
Unlike finding just ONE path, here we:
- Continue exploring even after finding a path
- Store ALL successful paths in a list
- Try directions in lexicographical order: D, L, R, U
- Must backtrack after each complete path to explore alternatives
Time Complexity
O(4^(Nยฒ))
Worst case: 4 choices per cell
Space Complexity
O(Nยฒ)
Visited matrix + recursion stack
Direction Priority (Lexicographical Order)
- D (Down): row + 1, same column
- L (Left): same row, column - 1
- R (Right): same row, column + 1
- U (Up): row - 1, same column
This order ensures paths are generated in lexicographical order automatically!
๐ Step-by-Step Path Finding
Ready. Load a maze to start demo.
Maze Visualization
Start
Goal
Current
Path
Wall
Found Paths
Load maze to begin
Progress Tracker
1. Parse maze input
2. Initialize visited matrix
3. Start at (0,0)
4. Explore paths (DโLโRโU)
5. Record complete paths
6. Return all paths or -1
๐ฎ Solve Your Own Maze
Enter maze and press Find All Paths
Maze Visualization
All Found Paths
Test Cases
Sample 1
4ร4 maze โ Output: DDRDRR DRDDRR
4ร4 maze โ Output: DDRDRR DRDDRR
Sample 2
2ร2 blocked โ Output: -1
2ร2 blocked โ Output: -1
Sample 3
3ร3 maze โ Output: RDDR
3ร3 maze โ Output: RDDR
๐ Algorithm Analysis
Time
O(4^(Nยฒ))
Worst case explores all possibilities
Space
O(Nยฒ)
Visited matrix + recursion
Complexity Breakdown
- Time: Up to 4 directions per cell, Nยฒ cells โ O(4^(Nยฒ))
- Space: Visited matrix O(Nยฒ) + path string + recursion depth O(Nยฒ)
- Practical: Much faster due to walls and visited cells pruning search space
- Paths: Number of valid paths varies greatly based on maze structure
Why This Approach?
- Backtracking naturally explores all possibilities
- Lexicographical order achieved by trying DโLโRโU
- Visited matrix prevents cycles within a single path
- Backtracking allows exploring alternative routes
Key Implementation Points
- Mark cell as visited before exploring
- Unmark after returning (backtracking)
- Try directions in exact order: D, L, R, U
- Store complete path when reaching goal
- Continue exploring to find all paths
- Return sorted list (already sorted if directions ordered correctly)
Real-World Applications
- Robotics: Finding all possible navigation routes
- Games: Pathfinding with multiple solutions
- Network Routing: Discovering redundant paths
- Circuit Design: Multiple routing possibilities